package com.example.demo.设计模式.代理模式.动态代理.CGLIB代理;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @description:
 * @projectName:demo
 * @see:设计模式.代理模式.动态代理.CGLIB代理
 * @author:袁成龙
 * @createTime:2020/10/12 20:18
 * @version:1.0
 */
public class TestHeader<T> implements MethodInterceptor {

    private Object object;

    public TestHeader(Object object) {
        this.object = object;
    }

    public Enhancer enhancer = new Enhancer();

    public T getObject(Class<T> tClass){
        enhancer.setSuperclass(tClass);
        enhancer.setCallback(this);

        return (T)enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

        System.out.println("=======开始操作之前=======");

        Object invoke = methodProxy.invoke(object, objects);

        System.out.println("++++++++"+invoke);

        System.out.println("=======开始操作之后=======");

        return invoke;
    }
}